#!/bin/csh -f

# @configure_input@

#
# This script installs the FFT benchmarks for each FFT library
# 

set prefix=@prefix@
set datarootdir=@datarootdir@

set max_nthread = 1
set ncache = 1

# ensure that IPP library does not use multiple cores for single FFT
setenv OMP_NUM_THREADS 1

if ( -f /proc/cpuinfo ) then

  set noglob
  set eval=`grep 'cache size' /proc/cpuinfo | head -1 | awk -F: '{print $2}' | sed -e 's/KB/\* 1024/'`

  set chips=`grep "model name" /proc/cpuinfo | head -1 | awk '{print $7}'`
  echo Chip set: $chips
  if ( $chips == E5520 ) then
    echo Nehalem Level 3 cache shared by 4 cores
    set max_nthread = 8
    set ncache = 2
  else if ( $chips == E5345 ) then
    echo Clovertown Level 2 cache shared by 2 cores
    set max_nthread = 8
    set ncache = 4
  else if ( $chips == X7560 ) then
    echo Knights Ferry Level 3 cache shared by 8 cores
    set max_nthread = 32
    set ncache = 4
  else
    set max_nthread = `cat /proc/cpuinfo | grep processor | wc -l`
    set ncache = $max_nthread
  endif

  @ cache = $eval / 2
  unset noglob
else
  set cache = 1
  set max_nthread = `sysctl hw.ncpu | awk '{print $2}'`
endif

echo Maximum number of threads = $max_nthread
echo Streaming cache size = $ncache X $cache

foreach lib ( $* )

  set nthread = 1
  while ($nthread <= $max_nthread)

    set use_cache = $cache
    if ( $ncache != 0 && $nthread > $ncache ) then
      @ use_cache = ($cache * $ncache) / $nthread
    endif

    echo Threads: $nthread Cache: $use_cache

    @srcdir@/fft_bench --library=$lib --nthread=$nthread --cache=$use_cache --ncore=$max_nthread

    mv fft_bench.out @datadir@/fft_bench_${lib}_${nthread}.dat

    @ nthread = $nthread * 2

  end

end

